package 泛型中的上界下界;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Timestamp extends Date
 * Created by juntzhan on 2015/7/24.
 */
public class BoundTest {
    public static void main(String[] args) {

    }

    public void upperBound(List<? extends Date> list, Date date) {
        Date now = list.get(0);
        System.out.println("now==>" + now);
//        list.add(date); //这句话无法编译
        list.add(null);//这句可以编译，因为null没有类型信息
    }

    public <T extends Date> void upperBound2(List<T> list, T date) {
        list.add(date);
    }

    public void testUpperBound2() {
        List<Timestamp> list = new ArrayList<>();
        Date date = new Date();
        Timestamp time = new Timestamp(date.getTime());
        upperBound2(list, time);
        //upperBound2(list,date);//这句同样无法编译
    }


    public void lowerBound(List<? super Timestamp> list) {
        Timestamp now = new Timestamp(System.currentTimeMillis());
        list.add(now);
//        Timestamp time = list.get(0); //不能编译
    }

    public void testLowerBound() {
        List<Date> list = new ArrayList<Date>();
        list.add(new Date());
        lowerBound(list);
    }

}
